﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace A2DFramework.HttpLoadBalancer.HttpProtocal
{
    /// <summary>
    /// HTTP请求报文
    /// </summary>
    public class HttpRequestEntity
    {
        public HttpRequestEntity()
        {
            this.HttpHeaders = new HttpHeadersCollection();
        }

        /// <summary>
        /// 请求头，如：GET /file.aspx HTTP/1.1
        /// </summary>
        public RequestLine RequestLine { get; set; }


        /// <summary>
        /// HTTP头列表
        /// </summary>
        public HttpHeadersCollection HttpHeaders { get; set; }

        /// <summary>
        /// 通用HTTP头
        /// </summary>
        public HttpHeadersCollection GeneralHeaders
        {
            get
            {
                if (this.HttpHeaders == null)
                    return null;

                var q = from headers in this.HttpHeaders
                        where headers.HeaderType == HttpHeaderType.General
                        select headers;
                HttpHeadersCollection collection=new HttpHeadersCollection();
                collection.AddRange(q.ToList());
                return collection;
            }
        }

        /// <summary>
        /// 请求HTTP头
        /// </summary>
        public HttpHeadersCollection RequestHeaders
        {
            get
            {
                if (this.HttpHeaders == null)
                    return null;

                var q = from headers in this.HttpHeaders
                        where headers.HeaderType == HttpHeaderType.Request
                        select headers;
                HttpHeadersCollection collection=new HttpHeadersCollection();
                collection.AddRange(q.ToList());
                return collection;
            }
        }

        /// <summary>
        /// Entity HTTP头
        /// </summary>
        public HttpHeadersCollection EntityHeaders
        {
            get
            {
                if (this.HttpHeaders == null)
                    return null;

                var q = from headers in this.HttpHeaders
                        where headers.HeaderType == HttpHeaderType.Entity
                        select headers;
                HttpHeadersCollection collection=new HttpHeadersCollection();
                collection.AddRange(q.ToList());
                return collection;
            }
        }

        ///// <summary>
        ///// 消息体
        ///// </summary>
        //public string HttpBody { get; set; }

        public byte[] BodyBytes { get; set; }
    }

    /// <summary>
    /// 请求头，如：GET /file.aspx HTTP/1.1
    /// </summary>
    public struct RequestLine
    {
        /// <summary>
        /// GET/POST/DELETE/PUT/TRACE/CONNECT/OPTIONS
        /// </summary>
        public HttpRequestActions Action { get; set; }

        /// <summary>
        /// 路径
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// HTTP版本
        /// </summary>
        public HttpProtocalVersions Version { get; set; }
    }
    

    /// <summary>
    /// HTTP协议版本
    /// </summary>
    public enum HttpRequestActions
    {
        GET,
        POST,
        OPTIONS,
        HEAD,
        PUT,
        DELETE,
        TRACE,
        CONNECT,
        Unknow
    }
}
